package com.medical.controller;


import com.github.pagehelper.PageInfo;
import com.medical.entity.SysArea;
import com.medical.entity.SysNhorgan;
import com.medical.service.NhorganService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/nhorgan")
public class NhorganController {
    @Autowired
    private NhorganService nhorganService;
    /**
     * 获取农合机构信息列表，包含模糊查询，分页
     * @param currentPage
     * @param limit
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getList")
    public String getNhorganList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String nhorganName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysNhorgan> pageInfo = nhorganService.getNhorganList(currentPage, limit, nhorganName);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", nhorganName);
        return "/nhorgan/nhorgan_list";
    }

    @RequestMapping("/openAdd")
    public String openadd() {return "/nhorgan/nhorgan_add";}

    @RequestMapping("/openEdit")
    public String openEdit(Integer id,Model model) {
        SysNhorgan nhorgan = nhorganService.getNhorgan(id);

        model.addAttribute("nhorgan", nhorgan);
        return "/nhorgan/nhorgan_edit";
    }
    @RequestMapping("/add")
    public String add(SysNhorgan sysNhorgan,HttpServletRequest request, Model model){
        nhorganService.add(sysNhorgan);
        return getNhorganList(1, 10, request, model);
    }

    @RequestMapping("edit")
    public String edit(SysNhorgan nhorgan, HttpServletRequest request, Model model) {
        nhorganService.edit(nhorgan);
        return getNhorganList(1, 10, request, model);
    }

    @RequestMapping("delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        nhorganService.delete(id);
        return getNhorganList(1, 10, request,model);
    }

//    @ResponseBody
//    @RequestMapping("getNhorganTreeList")
//    public Object getAreaTreeList() {
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("nhorganList", nhorganService.getNhorganTree(""));
//        return map;
//    }
}
